package cn.jorian.framework.service.impl;

import cn.jorian.framework.bean.*;
import cn.jorian.framework.net.NetManager;
import cn.jorian.framework.service.IAdjustBackService;
import cn.jorian.framework.util.GsonUtil;
import cn.jorian.framework.util.ParamUtil;
import com.google.gson.reflect.TypeToken;
import org.springframework.stereotype.Service;
import retrofit2.Response;

import java.io.IOException;
import java.util.UUID;

@Service
public class AdjustBackServiceImpl implements IAdjustBackService {

    @Override
    public ResponseResult adjustBack() {
        AdjustBackBean adjustBackBean = new AdjustBackBean();
        adjustBackBean.setOwnerCode("19940");
        adjustBackBean.setAdjustmentNoWms("456789");
        adjustBackBean.setOmsOrderNo("123456");
        adjustBackBean.setAdjustType("22");
        adjustBackBean.setAdjustTime("2024-09-19");
        adjustBackBean.setAsnNo("asnno");
        adjustBackBean.setRemarks("备注");


        AdjustBackBean.DtlBean dtlBean = new AdjustBackBean.DtlBean();

        dtlBean.setAdjustmentNoWms(adjustBackBean.getAdjustmentNoWms());
        dtlBean.setAdjustDtlNo("123");
        dtlBean.setOmsOrderNo(adjustBackBean.getOmsOrderNo());
        dtlBean.setOmsOrderDtlNo("456");
        dtlBean.setApplicationNoDtl("789");
        dtlBean.setOwnerOgoodsCode("1000");
        dtlBean.setQty(2.0);
        dtlBean.setPrdDateOld("2024-09-19");
        dtlBean.setValidDateOld("2025-09-19");
        dtlBean.setBatchNbrOld("20240919");

        dtlBean.setAdjustType(adjustBackBean.getAdjustType());
        dtlBean.setCbCode("7");
        dtlBean.setAdjustReason("报损");
        dtlBean.setGoodsStatusOld("1");
        dtlBean.setGoodsStatusNew("2");
        adjustBackBean.addList(dtlBean);


        DataBean<AdjustBackBean> dataBean = new DataBean<>();
        dataBean.setInterfaceType("Adjustment");
        dataBean.setChannelType("cdfy-erp");
        dataBean.setInterfaceNbr(UUID.randomUUID().toString());
        dataBean.addRecord(adjustBackBean);

        String json = "{\"record\":{\"recordDtl\":{\"omsOrderNo\":\"109HKDJ250217000002\",\"adjustType\":\"1\",\"ownerCode\":\"39725\",\"adjustTime\":\"2025-02-19 16:15:01\",\"adjustmentNoWms\":\"109HKDJ250217000002\",\"applicationNo\":null,\"asnNo\":null,\"remarks\":null,\"dtlList\":[{\"adjustDtlNo\":\"1\",\"applicationNoDtl\":null,\"omsOrderDtlNo\":\"109HKDJ250217000001-1\",\"ownerOgoodsCode\":\"223600\",\"cbCode\":\"83805\",\"qty\":1,\"adjustReason\":\"4\",\"custCode\":null,\"prdBatchOld\":null,\"prdBatchNew\":null,\"batchNbrOld\":\"240445\",\"batchNbrNew\":\"240445\",\"goodsStatusOld\":\"3\",\"goodsStatusNew\":\"1\",\"prdDateOld\":\"2024-04-25\",\"prdDateNew\":\"2024-04-25\",\"validDateOld\":\"2027-02-31\",\"validDateNew\":\"2027-02-31\",\"approvalNumberOld\":null,\"approvalNumberNew\":null,\"registrationNumberOld\":null,\"registrationNumberNew\":null,\"srlNbrList\":null}]}},\"interfaceType\":\"Adjustment\",\"channelType\":\"NMCF-ERP\",\"trackNo\":\"109HKDJ250217000002\",\"interfaceNbr\":\"e322e1d7-29aa-42ce-9b12-e3a95dd685c1\"}";


        DataBean<AdjustBackBean> dataBean1 = GsonUtil.fromJson(json, new TypeToken<DataBean<AdjustBackBean>>(){});

        try {
            //同步post请求
            Response<ResponseResult> response = NetManager.newInstance()
                    .getServiceApi()
                    .indocBack(ParamUtil.getParam(dataBean1))
                    .execute();
            ResponseResult result = response.body();
            return result;
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseResult.fail(e.getMessage());
        }
    }
}
